package service.impl.v2;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.ApplyReturnVOMapper;
import dao.v2.ConfigVOMapper;
import dao.v2.FileVOMapper;
import dao.v2.MaterialVOMapper;
import dao.v2.MemberLevelItemVOMapper;
import model.FileVO;
import model.GoodStandardVO;
import model.GoodVO;
import model.MaterialVO;
import model.MemberVO;
import model.SeqVO;
import service.v2.MaterialService;
import util.NumberUtil;
import util.OssUtil;
import util.PropertieUtil;
@Service
public class MaterialServiceImpl implements MaterialService {
	@Autowired
	private MaterialVOMapper dao;
	@Autowired
	private MemberLevelItemVOMapper levelAwardDao;
	@Autowired
	private ApplyReturnVOMapper applyReturnVOMapper;
	@Autowired
	private FileVOMapper fileVOMapper;
	@Autowired
	private ConfigVOMapper configVOMapper;

	public int checkGtLevel(String member_id, String vip_level) {
		return dao.checkGtLevel(member_id, vip_level);
	}
	
	public void processImg(List<MaterialVO> materialList, String screenwidth, int i) {
		for (MaterialVO vo : materialList) {
			for (int j = 0; j < vo.getImgs().size(); j++) {
				String path = vo.getImgs().get(j);
				if(path != null && !"".equals(path)){
					path +=  "?x-oss-process=image/resize,w_"+Integer.parseInt(screenwidth)/i;
//					path +=  "?x-oss-process=image/resize,m_lfit,h_"+Integer.parseInt(screenwidth)/i+",w_"+Integer.parseInt(screenwidth)/i;
					vo.getImgs().set(j, path);
				}
			}
		}
	}

	public List<MaterialVO> getMaterialList(MaterialVO materialVO) {
		return dao.getMaterialList(materialVO);
	}

	public int getTotal(MaterialVO materialVO) {
		return dao.getTotal(materialVO);
	}

	public List<MaterialVO> processShareInfo(List<MaterialVO> materialList) throws Exception {
		if(materialList.size() != 0){
			for (MaterialVO materialVO : materialList) {
				GoodVO goodVO = materialVO.getGoodVO();
				if(goodVO != null){
					goodVO = processShareInfo(goodVO);
					materialVO.setShare_info(goodVO.getStandard_list().get(0).getShare_info());
				}
			}
		}
		return materialList;
	}
	
	public GoodVO processShareInfo(GoodVO goodVO) throws Exception {
		String share_cash = "0";
		if("Y".equals(goodVO.getJoin_award())){
			share_cash = goodVO.getJoin_award_money();
		}
		String minRate = levelAwardDao.getMinRate();
		String maxRate = levelAwardDao.getMaxRate();
		for (GoodStandardVO standardVO : goodVO.getStandard_list()) {
			if(PropertieUtil.loadPropertie("buy_type_cash").equals(goodVO.getBuy_type())){
				standardVO.setShare_info(NumberUtil.formatDouble(Double.parseDouble(share_cash), 2));
				standardVO.setMin_share_info(NumberUtil.formatDouble(Double.parseDouble(share_cash), 2));
			}else{
				standardVO.setShare_info(NumberUtil.formatDouble(Double.parseDouble(share_cash)/*+Integer.parseInt(standardVO.getTicket())*(1-Double.parseDouble(maxRate))*/, 2)
						+"~"+NumberUtil.formatDouble(Double.parseDouble(share_cash)
						+Integer.parseInt(standardVO.getTicket())*(1-Double.parseDouble(minRate)), 2));
				standardVO.setMin_share_info(NumberUtil.formatDouble(Double.parseDouble(share_cash)/*+Integer.parseInt(standardVO.getTicket())*(1-Double.parseDouble(maxRate))*/ , 2));
			}
		}
		return goodVO;
	 }
	
	public void insertMaterial(MaterialVO materialVO, String files, String material_cat) {
		materialVO.setMaterial_cat(material_cat);
		materialVO.setImgs(Arrays.asList(files.split(",")));
		SeqVO seqVO = new SeqVO();
		applyReturnVOMapper.getSeq(seqVO);
		materialVO.setFolder_id(seqVO.getSeq());
		//插入数据
		dao.insertMaterial(materialVO);
		//插入文件
		fileVOMapper.insertFiles(materialVO);
		//fileVOMapper.insertFolder_File(materialVO);
	}
	
	public MaterialVO checkMaterial(MaterialVO materialVO) {
		return dao.checkMaterial( materialVO) ;
	}
	
	public void backupAndDelete(String id) {
		MaterialVO materialVO = dao.getMaterialById(id);
		if("2".equals(materialVO.getState())){
			//备份素材
			dao.backMaterial(id);
		}else{
			//删除素材
			
			//1.删文件
			List<FileVO> files = fileVOMapper.getFilesByFolder(materialVO.getFolder_id());
			for (FileVO fileVO : files) {
				OssUtil.deleteFile(fileVO.getPath());
			}
			//2.删file和folder表
			fileVOMapper.deleteFiles(materialVO.getFolder_id());
			//fileVOMapper.deleteFolder(materialVO.getFolder_id());
			
			//3.删素材表
			dao.deleteMaterial(id);
		}
	}
	
	public List<MaterialVO> addSellUrl(List<MaterialVO> materialList, MemberVO memberVO) throws Exception {
		if(materialList.size()!=0 && memberVO != null){
			for (MaterialVO materialVO : materialList) {
				if("1".equals(materialVO.getCat())){
					materialVO.setShare_url(configVOMapper.loadConfig("WECHAT_HOST")+"/goodDetail?good_id="+materialVO.getGood_id()+"&ym_share_inviteno="+memberVO.getInvite_no());
				}else if("2".equals(materialVO.getCat())){
					materialVO.setUrl(materialVO.getUrl()+"&ym_share_inviteno="+memberVO.getInvite_no());
				}
			}
		}
		return materialList;
	}
	public  Map newProcessShareInfo(String join_award,String join_award_money,String buy_type,String ticket) {
	
		Map map  = new  HashMap();
		String share_cash = "0";
		if("Y".equals(join_award)){
			share_cash = join_award_money;
		}
		String minRate = levelAwardDao.getMinRate();
		String maxRate = levelAwardDao.getMaxRate();
		try{
			if(PropertieUtil.loadPropertie("buy_type_cash").equals(buy_type)){
				map.put("share_info", NumberUtil.formatDouble(Double.parseDouble(share_cash), 2));
				map.put("min_share_info", NumberUtil.formatDouble(Double.parseDouble(share_cash), 2));
			}else{
				map.put("share_info",NumberUtil.formatDouble(Double.parseDouble(share_cash)/* +Integer.parseInt(ticket)*(1-Double.parseDouble(maxRate))*/, 2)
						
						+"~"+NumberUtil.formatDouble(Double.parseDouble(share_cash)
						+Integer.parseInt(ticket)*(1-Double.parseDouble(minRate)), 2));
				map.put("min_share_info", NumberUtil.formatDouble(Double.parseDouble(share_cash)/*+Integer.parseInt(ticket)*(1-Double.parseDouble(maxRate))*/ , 2));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	 }
	
}
